package map_;

import java.util.*;

/**
 * @author DCSGO
 * @version 1.0
 * Map的遍历
 */

@SuppressWarnings({"all"})
public class MapFor {
    public static void main(String[] args) {
        Map map = new HashMap();
        map.put("小米", 18);
        map.put("华为", 28);
        map.put("oppo", 25);

        //第一组：取出所有 key 再通过 key 获取对应的 value
        //(1). foreach 遍历
        for (Object key : map.keySet()) {
            System.out.print(key + "-" + map.get(key) + " ");
        }//oppo-25 华为-28 小米-18

        System.out.println();
        //(2). Iterator 迭代器 遍历
        Iterator keyIte = map.keySet().iterator();
        while (keyIte.hasNext()) {
            Object key = keyIte.next();
            System.out.print(key + "-" + map.get(key) + " ");
        }//oppo-25 华为-28 小米-18


        System.out.println();
        //第二组：取出所有 value
        Collection values = map.values();
        //(1). foreach 遍历
        for (Object value : values) {
            System.out.print(value + " ");
        }//25 28 18

        System.out.println();
        //(2).Iterator 迭代器遍历
        Iterator valIte = map.values().iterator();
        while (valIte.hasNext()) {
            Object value = valIte.next();
            System.out.print(value + " ");
        }//25 28 18

        System.out.println();
        //第三组：通过 EntrySet 来遍历
        Set entrySet = map.entrySet();
        //(1). foreach 遍历
        for (Object elem : entrySet) {
            Map.Entry entry = (Map.Entry) elem;
            System.out.print(entry.getKey() + "-" + entry.getValue() + " ");
        }//oppo-25 华为-28 小米-18

        System.out.println();
        //(2). 迭代器遍历
        Iterator entIte = entrySet.iterator();
        while (entIte.hasNext()) {
            Map.Entry entry = (Map.Entry) entIte.next();
            System.out.print(entry.getKey() + "-" + entry.getValue() + " ");
        }//oppo-25 华为-28 小米-18
    }

}
